//===============================================================================
// XGenPlus Tool
// ==============================================================================
// Please visit http://www.codeplex.com/xgenplus for latest updates.
//
// This source is subject to the GNU General Public License v2.
// 
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.CodeDom;
using Microsoft.CSharp;
using System.Collections;
using System.CodeDom.Compiler;
using System.Xml;
using System.Xml.Serialization;
using Config;

namespace XGenPlus.Config
{
    class Util
    {
        /// <summary>
        /// Writes the configuration to a config file
        /// </summary>
        /// <param name="p">The parameters to write</param>
        /// <param name="file">The file to write</param>
        public static void PutConfig(Parameters p, string file)
        {
             XGenPlusConfig conf = new  XGenPlusConfig();
             GenerateFor gen = new  GenerateFor();

            gen.Assembly = p.AssemblyName;
            gen.CopyTo = p.CopyTo;
            gen.Nocompile = p.NoCompile;
            gen.Nogenerate = p.NoGenerate;

            if (null != p.ReferenceList)
                for (int i = 0; i < p.ReferenceList.Count; i++)
                {
                     Add a = new  Add();
                    a.Command =  AddCommand.Reference;
                    a.Value = p.ReferenceList[i];
                    gen.AddCollection.Add(a);
                }

            if (null != p.IncludeList)
                for (int i = 0; i < p.IncludeList.Count; i++)
                {
                     Add a = new  Add();
                    a.Command =  AddCommand.Include;
                    a.Value = p.IncludeList[i];
                    gen.AddCollection.Add(a);
                }


            if (null != p.ExcludeList)
                for (int i = 0; i < p.ExcludeList.Count; i++)
                {
                     Add a = new  Add();
                    a.Command =  AddCommand.Exclude;
                    a.Value = p.ExcludeList[i];
                    gen.AddCollection.Add(a);
                }

            conf.Add(gen);

            using (StreamWriter writer = new StreamWriter(file))
            {
                XmlSerializer ser = new XmlSerializer(typeof( XGenPlusConfig));
                ser.Serialize(writer, conf);
            }



        }

        /// <summary>
        /// Reads the configuration from a XGenPlus configuration file
        /// </summary>
        /// <param name="file">The configuration file path</param>
        /// <returns></returns>
        public static Parameters GetConfig(string file)
        {
            Parameters p = new Parameters();
             XGenPlusConfig conf = new  XGenPlusConfig();

            XmlSerializer ser = new XmlSerializer(typeof( XGenPlusConfig));
            conf = ( XGenPlusConfig)ser.Deserialize(new StreamReader(file));

             GenerateFor gen = conf.GenerateForCollection[0];

            p.AssemblyName = gen.Assembly;
            p.CopyTo = gen.CopyTo;
            p.NoCompile = gen.Nocompile;
            p.NoGenerate = gen.Nogenerate;

            if (null != gen.AddCollection)
            {
                foreach ( Add a in gen.AddCollection)
                {
                    if (a.Command ==  AddCommand.Exclude)
                        p.ExcludeList.Add(a.Value);
                    if (a.Command ==  AddCommand.Include)
                        p.IncludeList.Add(a.Value);
                    if (a.Command ==  AddCommand.Reference)
                        p.ReferenceList.Add(a.Value);
                }
            }

            return p;

        }
    }
}
